import { Component, OnInit } from '@angular/core';
import {ExtendHttpService} from "../../../common/extend-http.service";
import {Router} from "@angular/router";
import {TipService} from "../../services/tip.service";

@Component({
  selector: 'app-dishes-count',
  templateUrl: './dishes-count.component.html',
  styleUrls: ['./dishes-count.component.css']
})
export class DishesCountComponent implements OnInit {
  loading = true;
  isLoading = false;
  isFlag = false;
  tipType = 'error';
  totalPage = 1;
  loadMoreFlag = true;
  list = [];
  params = {start: 1, startTime: '', endTime: ''}
  constructor(
    private extendHttp: ExtendHttpService,
    private router: Router,
    private tipService: TipService
  ) { }

  ngOnInit() {
    // input为date的元素的特殊处理 格式为: 2018-02-25
    var nowEnd = new Date();
    var monthEnd = (nowEnd.getMonth() + 1);
    var day = nowEnd.getDate();
    var m = [];
    if (monthEnd < 10) {
      m.push(0);
      m.push(monthEnd);
    } else {
      m.push(monthEnd);
    }
    var d = []
    if (day < 10) {
      d.push(0);
      d.push(day);
    } else {
      d.push(day);
    }
    var today = nowEnd.getFullYear() + '-' + m.join('') + '-' + d.join('');
    this.params.endTime = today;

    var nowStart = new Date();
    nowStart.setMonth(nowStart.getMonth() - 1);
    var monthStart = nowStart.getMonth() + 1;
    var dayStart = nowStart.getDate();

    var m1 = [];
    var d1 = [];
    if (monthStart < 10){
      m1.push(0);
      m1.push(monthStart);
    }
    if (dayStart < 10) {
      d1.push(0);
      d1.push(dayStart);
    } else {
      d1.push(dayStart)
    }
    var todayStart = nowEnd.getFullYear() + '-' + m1.join('') + '-' + d1.join('');
    this.params.startTime = todayStart;
    cBack = () => {
      $('#btn-back').trigger('click');
    }
    this.getData();
  }

  getData(searchFlag?: boolean) {
    if (searchFlag) {
      if (!this.params.startTime.trim()) {
        this.tipService.change(true, '请选择开始时间');
        return;
      }
      if (!this.params.endTime.trim()) {
        this.tipService.change(true, '请选择结束时间');
        return;
      }
    }

    this.extendHttp.post('/merchantApp/Statistics/saleOfDishes', this.params).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.list = data.page.list;
        this.params.start = data.page.pageNumber;
        this.totalPage = data.page.totalPage;
        this.isFlag = true;
        this.loading = false;
      } else if (data.code == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.loading = false;
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.loading = false;
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

  loadMore() {

    if (this.loadMoreFlag) {
      this.params.start = this.params.start + 1;
      this.loadMoreFlag = false;
      this.extendHttp.post('/merchantApp/Statistics/saleOfDishes', this.params).subscribe(response => {
        let data = response.json();
        if (data.code == '0') {
          this.list = this.list.concat(data.page.list);
          this.params.start = data.page.pageNumber;
          this.totalPage = data.page.totalPage;
          this.loadMoreFlag = true;
        } else if (data.code == '-2001') {
          this.tipType = 'error';
          this.tipService.change(true, '登录超时，跳转到登录页面');
          setTimeout(() => {
            this.router.navigate(['/login']);
          }, 2000);
        } else {
          this.params.start = this.params.start - 1;
          this.tipType = 'error';
          this.tipService.change(true, data.message);
          this.loadMoreFlag = true;
        }
      }, error => {
        this.tipType = 'error';
        this.loadMoreFlag = true;
        this.params.start = this.params.start - 1;
        this.tipService.change(true, '系统繁忙，操作失败！');
      });
    }
  }

  clearData() {
    this.params.startTime = '';
    this.params.endTime = ''
  }

}
